<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>浏览器导航</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            /* 明亮模式变量 */
            --bg-gradient-start: #667eea;
            --bg-gradient-end: #764ba2;
            --text-primary: #333;
            --text-secondary: #666;
            --card-bg: rgba(255, 255, 255, 0.95);
            --card-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            --item-bg: #f8f9fa;
            --border-color: #e0e0e0;
            --time-color: white;
            --footer-color: rgba(255, 255, 255, 0.8);
        }

        @media (prefers-color-scheme: dark) {
            :root {
                /* 暗黑模式变量 */
                --bg-gradient-start: #1a1a2e;
                --bg-gradient-end: #16213e;
                --text-primary: #e0e0e0;
                --text-secondary: #b0b0b0;
                --card-bg: rgba(30, 30, 46, 0.95);
                --card-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
                --item-bg: #2a2a3e;
                --border-color: #444;
                --time-color: #e0e0e0;
                --footer-color: rgba(224, 224, 224, 0.8);
            }
        }

        body {
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, var(--bg-gradient-start) 0%, var(--bg-gradient-end) 100%);
            min-height: 100vh;
            color: var(--text-primary);
            transition: all 0.3s ease;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
        }

        .time-display {
            font-size: 3rem;
            font-weight: 300;
            color: var(--time-color);
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
            margin-bottom: 10px;
        }

        .date-display {
            font-size: 1.2rem;
            color: var(--footer-color);
            margin-bottom: 30px;
        }

        .search-section {
            background: var(--card-bg);
            border-radius: 20px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: var(--card-shadow);
        }

        .search-engine-selector {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
            gap: 10px;
        }

        .engine-btn {
            padding: 8px 16px;
            border: 2px solid var(--border-color);
            background: var(--card-bg);
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
            color: var(--text-primary);
        }

        .engine-btn.active {
            background: var(--bg-gradient-start);
            color: white;
            border-color: var(--bg-gradient-start);
        }

        .search-box {
            display: flex;
            max-width: 600px;
            margin: 0 auto;
        }

        .search-input {
            flex: 1;
            padding: 15px 20px;
            border: 2px solid var(--border-color);
            border-radius: 25px 0 0 25px;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s ease;
            background: var(--card-bg);
            color: var(--text-primary);
        }

        .search-input:focus {
            border-color: var(--bg-gradient-start);
        }

        .search-btn {
            padding: 15px 25px;
            background: var(--bg-gradient-start);
            color: white;
            border: none;
            border-radius: 0 25px 25px 0;
            cursor: pointer;
            font-size: 16px;
            transition: background 0.3s ease;
        }

        .search-btn:hover {
            background: var(--bg-gradient-end);
        }

        .content-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .card {
            background: var(--card-bg);
            border-radius: 15px;
            padding: 25px;
            box-shadow: var(--card-shadow);
        }

        .card h3 {
            margin-bottom: 20px;
            color: var(--bg-gradient-start);
            border-bottom: 2px solid var(--border-color);
            padding-bottom: 10px;
        }

        .quick-links {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
            gap: 15px;
        }

        .link-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            text-decoration: none;
            color: var(--text-primary);
            padding: 15px;
            border-radius: 10px;
            transition: all 0.3s ease;
            background: var(--item-bg);
        }

        .link-item:hover {
            background: var(--bg-gradient-start);
            color: white;
            transform: translateY(-2px);
        }

        .link-icon {
            font-size: 24px;
            margin-bottom: 8px;
        }

        .tools-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
            gap: 15px;
        }

        .tool-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
            background: var(--item-bg);
            border-radius: 10px;
            text-decoration: none;
            color: var(--text-primary);
            transition: all 0.3s ease;
        }

        .tool-item:hover {
            background: var(--bg-gradient-start);
            color: white;
            transform: translateY(-2px);
        }

        .bookmarks-list {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        .bookmark-item {
            display: flex;
            align-items: center;
            padding: 12px 15px;
            background: var(--item-bg);
            border-radius: 8px;
            text-decoration: none;
            color: var(--text-primary);
            transition: all 0.3s ease;
        }

        .bookmark-item:hover {
            background: var(--bg-gradient-start);
            color: white;
        }

        .bookmark-icon {
            margin-right: 10px;
            font-size: 18px;
        }

        .add-item-btn {
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 12px 15px;
            background: var(--item-bg);
            border: 2px dashed var(--border-color);
            border-radius: 8px;
            color: var(--text-secondary);
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 10px;
        }

        .add-item-btn:hover {
            border-color: var(--bg-gradient-start);
            color: var(--bg-gradient-start);
        }

        .user-info {
            position: absolute;
            top: 20px;
            right: 20px;
            color: var(--time-color);
            font-size: 14px;
        }

        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 5px 10px;
            border-radius: 15px;
            cursor: pointer;
            margin-left: 10px;
            font-size: 12px;
        }

        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        @media (max-width: 768px) {
            .time-display {
                font-size: 2rem;
            }

            .content-grid {
                grid-template-columns: 1fr;
            }

            .user-info {
                position: static;
                text-align: center;
                margin-bottom: 20px;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        {if condition="is_logged_in()"}
        <div class="user-info">
            欢迎，{$Think.session.username}
            <button class="logout-btn" onclick="logout()">退出</button>
        </div>
        {else /}
        <div class="user-info">
            游客模式
            <button class="logout-btn" onclick="window.location.href='/user/login'">登录</button>
        </div>
        {/if}

        <div class="header">
            <div class="time-display" id="currentTime">00:00:00</div>
            <div class="date-display" id="currentDate">2024年1月1日 星期一</div>
        </div>

        <div class="search-section">
            <div class="search-engine-selector">
                <button class="engine-btn active" data-engine="bing">必应</button>
                <button class="engine-btn" data-engine="google">谷歌</button>
                <button class="engine-btn" data-engine="baidu">百度</button>
                <button class="engine-btn" data-engine="github">GitHub</button>
            </div>
            <div class="search-box">
                <input type="text" class="search-input" id="searchInput" placeholder="在必应中搜索...">
                <button class="search-btn" onclick="performSearch()">搜索</button>
            </div>
        </div>

        <div class="content-grid">
            <div class="card">
                <h3>快捷导航</h3>
                <div class="quick-links" id="quickLinksContainer">
                    {volist name="quickLinks" id="link"}
                    <a href="{$link.url}" class="link-item" target="_blank">
                        <div class="link-icon">{$link.icon}</div>
                        <span>{$link.title}</span>
                    </a>
                    {/volist}
                </div>
                {if condition="is_logged_in()"}
                <div class="add-item-btn" onclick="showAddForm('quickLink')">
                    + 添加快捷导航
                </div>
                {/if}
            </div>

            <div class="card">
                <h3>常用工具</h3>
                <div class="tools-grid" id="toolsContainer">
                    {volist name="tools" id="tool"}
                    <a href="{$tool.url}" class="tool-item" target="_blank">
                        <div class="link-icon">{$tool.icon}</div>
                        <span>{$tool.title}</span>
                    </a>
                    {/volist}
                </div>
                {if condition="is_logged_in()"}
                <div class="add-item-btn" onclick="showAddForm('tool')">
                    + 添加工具
                </div>
                {/if}
            </div>

            <div class="card">
                <h3>我的收藏</h3>
                <div class="bookmarks-list" id="bookmarksContainer">
                    {volist name="bookmarks" id="bookmark"}
                    <a href="{$bookmark.url}" class="bookmark-item" target="_blank">
                        <div class="bookmark-icon">{$bookmark.icon}</div>
                        <span>{$bookmark.title}</span>
                    </a>
                    {/volist}
                </div>
                {if condition="is_logged_in()"}
                <div class="add-item-btn" onclick="showAddForm('bookmark')">
                    + 添加收藏
                </div>
                {/if}
            </div>
        </div>
    </div>

    <!-- 添加项目的模态框 -->
    <div id="addModal"
        style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 1000;">
        <div
            style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; padding: 30px; border-radius: 15px; width: 90%; max-width: 400px;">
            <h3 id="modalTitle">添加项目</h3>
            <form id="addForm">
                <input type="hidden" id="itemType" name="type">
                <div style="margin-bottom: 15px;">
                    <label>标题</label>
                    <input type="text" id="itemTitle" name="title"
                        style="width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 5px;">
                </div>
                <div style="margin-bottom: 15px;">
                    <label>URL</label>
                    <input type="url" id="itemUrl" name="url"
                        style="width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 5px;">
                </div>
                <div style="margin-bottom: 15px;">
                    <label>图标</label>
                    <input type="text" id="itemIcon" name="icon" placeholder="🔍"
                        style="width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 5px;">
                </div>
                <div style="display: flex; gap: 10px;">
                    <button type="button" onclick="hideAddForm()"
                        style="flex: 1; padding: 10px; background: #f0f0f0; border: none; border-radius: 5px;">取消</button>
                    <button type="submit"
                        style="flex: 1; padding: 10px; background: #667eea; color: white; border: none; border-radius: 5px;">添加</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 更新时间显示
        function updateTime() {
            const now = new Date();
            const timeString = now.toLocaleTimeString('zh-CN');
            const dateString = now.toLocaleDateString('zh-CN', {
                year: 'numeric',
                month: 'long',
                day: 'numeric',
                weekday: 'long'
            });

            document.getElementById('currentTime').textContent = timeString;
            document.getElementById('currentDate').textContent = dateString;
        }

        // 搜索引擎配置
        const searchEngines = {
            bing: {
                name: '必应',
                url: 'https://www.bing.com/search?q=',
                placeholder: '在必应中搜索...'
            },
            google: {
                name: '谷歌',
                url: 'https://www.google.com/search?q=',
                placeholder: '在Google中搜索...'
            },
            baidu: {
                name: '百度',
                url: 'https://www.baidu.com/s?wd=',
                placeholder: '在百度中搜索...'
            },
            github: {
                name: 'GitHub',
                url: 'https://github.com/search?q=',
                placeholder: '在GitHub中搜索...'
            }
        };

        let currentEngine = 'bing';

        // 切换搜索引擎
        document.querySelectorAll('.engine-btn').forEach(btn => {
            btn.addEventListener('click', function () {
                document.querySelectorAll('.engine-btn').forEach(b => b.classList.remove('active'));
                this.classList.add('active');
                currentEngine = this.dataset.engine;
                const engine = searchEngines[currentEngine];
                document.getElementById('searchInput').placeholder = engine.placeholder;
            });
        });

        // 执行搜索
        function performSearch() {
            const query = document.getElementById('searchInput').value.trim();
            if (query) {
                const searchUrl = searchEngines[currentEngine].url + encodeURIComponent(query);
                window.open(searchUrl, '_blank');
            }
        }

        // 回车键搜索
        document.getElementById('searchInput').addEventListener('keypress', function (e) {
            if (e.key === 'Enter') {
                performSearch();
            }
        });

        // 显示添加表单
        function showAddForm(type) {
            const modal = document.getElementById('addModal');
            const title = document.getElementById('modalTitle');
            const typeInput = document.getElementById('itemType');

            typeInput.value = type;

            switch (type) {
                case 'quickLink':
                    title.textContent = '添加快捷导航';
                    break;
                case 'tool':
                    title.textContent = '添加工具';
                    break;
                case 'bookmark':
                    title.textContent = '添加收藏';
                    break;
            }

            modal.style.display = 'block';
        }

        // 隐藏添加表单
        function hideAddForm() {
            document.getElementById('addModal').style.display = 'none';
            document.getElementById('addForm').reset();
        }

        // 处理添加表单提交
        document.getElementById('addForm').addEventListener('submit', function (e) {
            e.preventDefault();

            const formData = new FormData(this);
            const data = Object.fromEntries(formData);

            let url = '';
            switch (data.type) {
                case 'quickLink':
                    url = '/home/addquicklink';
                    break;
                case 'tool':
                    url = '/home/addtool';
                    break;
                case 'bookmark':
                    url = '/home/addbookmark';
                    break;
            }

            fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(data)
            })
                .then(response => response.json())
                .then(result => {
                    if (result.code === 200) {
                        alert(result.message);
                        hideAddForm();
                        location.reload(); // 刷新页面显示新数据
                    } else {
                        alert(result.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('添加失败，请稍后重试');
                });
        });

        // 退出登录
        function logout() {
            if (confirm('确定要退出登录吗？')) {
                fetch('/user/logout', {
                    method: 'POST'
                })
                    .then(response => response.json())
                    .then(result => {
                        if (result.code === 200) {
                            window.location.href = '/user/login';
                        }
                    });
            }
        }

        // 初始化时间显示
        updateTime();
        setInterval(updateTime, 1000);

        // 点击模态框背景关闭
        document.getElementById('addModal').addEventListener('click', function (e) {
            if (e.target === this) {
                hideAddForm();
            }
        });
    </script>
</body>

</html>